package List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/5/12
 * \* Time: 19:36
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class ListNode {
    //结点的值
    int val;
    //下一个结点
    ListNode next;
    //结点的构造函数
    public ListNode(){

    }
    //结点的构造函数，有一个参数
    public ListNode(int val){
        this.val = val;
    }
    //结点的构造函数，有两个参数
    public ListNode(int val, ListNode next){
        this.val = val;
        this.next = next;
    }
    //LeetCode 203 移除链表元素
    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val){
            head = head.next;
        }
        if (head == null){
            return head;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while (cur != null){
            if(cur.val == val){
                pre.next = cur.next;
            }else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}
